package com.liupengyuan.framework.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestEncodeFilter implements Filter {
	private FilterConfig config = null;
	private String targetEncoding = "UTF-8";
	
	public void destroy() {
		this.targetEncoding = null;
		this.config = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest _request = (HttpServletRequest)request;
		HttpServletResponse _response = (HttpServletResponse)response;
		_request.setCharacterEncoding(targetEncoding);
		chain.doFilter(_request, _response);
	}

	public void init(FilterConfig config) throws ServletException {
		this.config = config;
		String targetEncoding = this.config.getInitParameter("encoding");
		if ((targetEncoding != null) && (targetEncoding.trim().length() > 0)){
			this.targetEncoding = targetEncoding;
		}
	}


}
